package com.bai.ucenter.controller;


import com.bai.ucenter.entity.Member;
import com.bai.ucenter.entity.vo.MemberVo;
import com.bai.ucenter.service.MemberService;
import com.bai.utils.JwtUtils;
import com.bai.utils.R;
import com.bai.utils.orderVo.UcenterMemberOrder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author bai
 * @since 2023-08-14
 */
@RestController
@RequestMapping("/ucenter/member")
@CrossOrigin
@Api("用户登录接口")
public class MemberController {

    @Autowired
    private MemberService memberService;

    //登录
    @ApiOperation(value = "登录")
    @PostMapping("login")
    public R login(@RequestBody Member member) {
        String token = memberService.login(member);
        return R.ok().data("token", token);
    }

    //注册
    @ApiOperation(value = "注册")
    @PostMapping("register")
    public R register(@RequestBody MemberVo memberVo) {
        memberService.register(memberVo);
        return R.ok();
    }

    //根据token获取用户信息
    @ApiOperation(value = "根据token获取用户信息")
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        Member member = memberService.getById(id);
        return R.ok().data("member", member);
    }

    //根据用户id获取用户信息
    @ApiOperation(value = "根据用户id获取用户信息")
    @PostMapping("getById/{memberId}")
    public UcenterMemberOrder getById(@PathVariable String memberId) {
        Member member = memberService.getById(memberId);
        UcenterMemberOrder memberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member, memberOrder);
        return memberOrder;
    }

    //TODO:查询某一天注册人数
    @GetMapping("cuntRegister/{day}")
    public R cuntRegister(@PathVariable String day) {
        Integer count = memberService.cuntRegister(day);
        return R.ok().data("countRegister",count);
    }
}

